using System.Collections.Generic;
using System.Linq;
using Voodoo.Sauce.Internal.Extension;
using Voodoo.Tune.Core;

namespace Voodoo.Sauce.Internal.VoodooTune
{
	internal class DebugVTConfiguration
	{
		private const string TAG = "DebugVTConfiguration";

		internal Sandbox SelectedSandbox { get; set; }

		internal List<Segment> SelectedSegments { get; set; } = new List<Segment>();


		internal List<ABTest> SelectedAbTests { get; set; } = new List<ABTest>();


		internal List<Cohort> SelectedCohorts { get; set; } = new List<Cohort>();


		internal string VersionId { get; set; }

		internal bool IsDefaultConfiguration
		{
			get
			{
				if (SelectedSandbox == null && SelectedCohorts.Count == 0 && SelectedAbTests.Count == 0 && SelectedSegments.Count == 0)
				{
					return string.IsNullOrEmpty(VersionId);
				}
				return false;
			}
		}

		internal bool IsSandbox
		{
			get
			{
				if (SelectedSandbox != null && SelectedCohorts.Count == 0 && SelectedAbTests.Count == 0 && SelectedSegments.Count == 0)
				{
					return string.IsNullOrEmpty(VersionId);
				}
				return false;
			}
		}

		internal bool IsSimulation
		{
			get
			{
				if (SelectedSandbox == null && ((SelectedCohorts.Count > 0 && SelectedAbTests.Count > 0 && SelectedCohorts.Count == SelectedAbTests.Count) || SelectedSegments.Count > 0))
				{
					return !string.IsNullOrEmpty(VersionId);
				}
				return false;
			}
		}

		internal bool HasNoneSelected
		{
			get
			{
				if (SelectedAbTests.Count == 0 && SelectedCohorts.Count == 0)
				{
					return SelectedSegments.Count == 0;
				}
				return false;
			}
		}

		internal string SelectedAbTestsToString => "[" + string.Join(",", SelectedAbTests.Select((ABTest x) => x.Name)) + "]";

		internal string SelectedCohortsToString => "[" + string.Join(",", SelectedCohorts.Select((Cohort x) => x.Name)) + "]";

		internal string SelectedSegmentsToString => "[" + string.Join(",", SelectedSegments.Select((Segment x) => x.Name)) + "]";

		internal string SelectedABTestsAndCohortsToString
		{
			get
			{
				string text = "";
				for (int i = 0; i < SelectedAbTests.Count; i++)
				{
					text = text + SelectedAbTests[i].Name + " - " + SelectedCohorts[i].Name + "\n";
				}
				return text;
			}
		}

		internal bool IsValid
		{
			get
			{
				if (!IsDefaultConfiguration && !IsSandbox)
				{
					return IsSimulation;
				}
				return true;
			}
		}

		public override bool Equals(object obj)
		{
			if (obj is DebugVTConfiguration debugVTConfiguration)
			{
				bool num = object.Equals(debugVTConfiguration.SelectedSandbox, SelectedSandbox);
				bool flag = (debugVTConfiguration.VersionId ?? "") == (VersionId ?? "");
				bool flag2 = SelectedAbTests.SequenceEqualsIgnoreOrder(debugVTConfiguration.SelectedAbTests, (ABTest x) => x.Id);
				bool flag3 = SelectedCohorts.SequenceEqualsIgnoreOrder(debugVTConfiguration.SelectedCohorts, (Cohort x) => x.Id);
				bool flag4 = SelectedSegments.SequenceEqualsIgnoreOrder(debugVTConfiguration.SelectedSegments, (Segment x) => x.Id);
				return num && flag && flag2 && flag3 && flag4;
			}
			return false;
		}

		public override int GetHashCode()
		{
			return SelectedSandbox.GetHashCode();
		}

		internal void PersistOnPlayerPrefs()
		{
			if (IsDefaultConfiguration)
			{
				VoodooTunePersistentData.Clear();
				VoodooLog.LogDebug(Module.VOODOO_TUNE, "DebugVTConfiguration", "Clear VT debug configuration");
			}
			else if (IsSandbox)
			{
				VoodooTunePersistentData.Save(SelectedSandbox);
				VoodooTunePersistentData.Clear(VoodooTuneDataType.Segment | VoodooTuneDataType.Cohort | VoodooTuneDataType.Version);
				VoodooLog.LogDebug(Module.VOODOO_TUNE, "DebugVTConfiguration", "Saved VT debug configuration - Sandbox '" + SelectedSandbox.Name + "'");
			}
			else if (IsSimulation)
			{
				VoodooTunePersistentData.Clear(VoodooTuneDataType.Sandbox);
				if (SelectedCohorts.Count > 0)
				{
					VoodooTunePersistentData.Save(SelectedAbTests, SelectedCohorts);
				}
				else
				{
					VoodooTunePersistentData.Clear(VoodooTuneDataType.Cohort);
				}
				if (SelectedSegments.Count > 0)
				{
					VoodooTunePersistentData.Save(SelectedSegments);
				}
				else
				{
					VoodooTunePersistentData.Clear(VoodooTuneDataType.Segment);
				}
				VoodooTunePersistentData.SavedVersionId = VersionId;
				VoodooLog.LogDebug(Module.VOODOO_TUNE, "DebugVTConfiguration", "Saved VT debug configuration - Version '" + VersionId + "'" + ((SelectedCohorts.Count > 0) ? $" - Cohorts '{SelectedCohorts}'" : "") + ((SelectedSegments.Count > 0) ? $" - Segments '{SelectedSegments}'" : ""));
			}
		}

		internal static DebugVTConfiguration GetFromPlayerPrefs()
		{
			VoodooTunePersistentData.Load();
			return new DebugVTConfiguration
			{
				SelectedSandbox = VoodooTunePersistentData.SavedSandbox,
				SelectedAbTests = VoodooTunePersistentData.SavedABTests,
				SelectedCohorts = VoodooTunePersistentData.SavedCohorts,
				SelectedSegments = VoodooTunePersistentData.SavedSegments,
				VersionId = VoodooTunePersistentData.SavedVersionId
			};
		}
	}
}
